const QQMapWX = require('@/libs/map/qqmap-wx-jssdk.min.js')
const qqmapsdk = new QQMapWX({
	key: 'HBHBZ-OTBKB-ESWUO-JFH5E-HZQTS-BZF66'
})
// import {
// 	setToken
// } from '@/utils/auth';
// import {
// 	xcxlogin
// } from '@/apis/login.js'


/**
 * 逆地址解析（坐标转具体位置信息）
 * @doc 文档参考：https://lbs.qq.com/qqmap_wx_jssdk/method-reverseGeocoder.html
 * @export
 * @param {*} location 坐标：{ latitude: 39.984060, longitude: 116.307520 }
 * @returns
 */
export function reverseGeocoder(location) {
	return new Promise((resolve, reject) => {
		qqmapsdk.reverseGeocoder({
			location: location,
			get_poi: 1,
			poi_options: 'policy=1;page_size=20;page_index=1',
			success: res => {
				resolve(res)
			},
			fail: err => {
				reject(err)
				uni.showToast({
					title: err.message,
					icon: 'none',
					duration: 3000
				})
			}
		})
	})
}



/**
 * 地图关键词搜索
 * @doc 文档参考：https://lbs.qq.com/qqmap_wx_jssdk/method-search.html
 * @export
 * @param {*} region 地区
 * @param {*} keyword 搜索关键词
 * @param {*} location 坐标：{ latitude: 39.984060, longitude: 116.307520 }
 * @returns
 */
export function mapSearch(region,keyword, location) {
	return new Promise((resolve, reject) => {
		qqmapsdk.search({
			region: region,
			keyword: keyword,
			location: location,
			page_size: 20,
			auto_extend: 0,
			success: res => {
				resolve(res)
			},
			fail: err => {
				reject(err)
				uni.showToast({
					title: err.message,
					icon: 'none',
					duration: 3000
				})
			}
		})
	})
}


/**
 * 重新授权并调用定位方法
 * @param {Function} successCb 授权成功回调
 * @param {Function} authDenyCb 授权失败回调
 */
export function getAuthorize(successCb, authDenyCb) {
	return new Promise((resolve, reject) => {
	uni.authorize({
		scope: 'scope.userLocation',
		success: () => {
			uni.showToast({
				title: '授权成功',
				icon: 'none'
			})
			resolve({result:true})
		},
		fail: (err) => {
			err = err['errMsg']
			uni.showModal({
				content: '需要授权位置信息',
				confirmText: '确认授权'
			}).then((res) => {
				if (res[1]['confirm']) {
					uni.openSetting({
						success: (res) => {
							if (res.authSetting['scope.userLocation']) {
								// 授权成功
								resolve({result:true})
							} else {
								// 未授权
								uni.showToast({
									title: '授权失败',
									icon: 'none'
								})
								resolve({result:false})
							}

						}
					})
				}
				if (res[1]['cancel']) {
					// 取消授权
					console.log('取消')
					resolve({result:false})

				}
			})
		}
	})
	})
}

/**
 * 获取用户当前位置信息
 *
 * @export
 */
export function getLocation() {
	return new Promise((resolve, reject) => {
		const scope = 'scope.userLocation'
		const modal = {
			title: '授权',
			content: '需要您授权使用位置信息',
			confirmText: '设置'
		}
		setAuthorize(scope, modal)
			.then(() => {
				uni.getLocation({
					altitude: true,
					success: res => {
						resolve(res)
					},
					fail: err => {
						console.log(err);
						reject(err)
						uni.showToast({
							title: '获取位置信息失败',
							icon: 'none',
							duration: 3000
						})
					}
				})
			})
			.catch(err => {
				reject(err)
			})
	})
}